<?php
// PHPViddlerUploadify
// Version 0.1
// By: Colin Devroe - cdevroe@viddler.com
// http://code.google.com/p/phpviddleruploadify/
// http://developers.viddler.com/


	// Initiate PHPViddlerUploadify class (which extends PHPViddler)
	include('phpviddler/phpviddlerUploadify.php');
	$viddler = new PhpviddlerUploadify();
	
	// Configuration options (required)
	
	$viddler->apiKey = ''; // API Key (log in, click Settings)
	$viddlerUsername = ''; // Your Viddler username
	$viddlerPassword = ''; // Your Viddler password
	$filedir = '/Users/cdevroe/Sites/viddler/phpviddleruploadify/uploads/'; // Directory that uploaded videos exist
	
	// END Configuration options
	
	
	// Do not time out.
	set_time_limit(0);
	
	// Be sure a video file name was submitted
	if (!$_POST['videofile']) { echo 'You must first upload a video. Please go back and try again.'; exit;}
	
	// Authenticate and get sessionID
	$a = $viddler->user_authenticate($viddlerUsername, $viddlerPassword);
	$sessionid = $a['auth']['sessionid'];
	
	// Upload the video from the directory to Viddler
	$response = $viddler->UploadToViddler($sessionid,$_POST['title'],$_POST['description'],$_POST['tags'],'1',$filedir.$_POST['videofile']);
	
	// Patch to handle Viddler API bug (will be fixed soon)
	if (!$response) {
		echo 'Due to a bug in the Viddler API - this means you did not send a file. Check your directory location and be sure the file uploaded successfully and that the path is correct.';
		exit;
	}
	
	// Did the API report an error?
	if ($response['error']) { ?>
		
		<h1>Submission Error</h1>
		<p><?php echo "There was a problem uploading your video, please try again. <br /> Error: ".$response['error']['description']; ?></p>

	
	<?php } else { // No errors
	
		// Delete the uploaded file
		@unlink($filedir.$_POST['videofile']);
		
		// Redirect back to form page with a thank you message
		// and send the URL for the video on Viddler
		header("Location:example.php?upload=success&url=".$response['video']['url']);
	
	} // end if error
?>